﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;
using Ninject;
using SomeRandomGame.Interfaces;
using SomeRandomGame.Utility;

namespace SomeRandomGame.Screens
{
    public class MainMenuScreen: MenuScreenBase
    {
        private IGameManager _gameManager;
        private SoundEffectInstance soundEffectInstance;

        public MainMenuScreen( IGameManager gameManager ): base( gameManager, Constants.Strings.MAIN_MENU )
        {
            _gameManager = gameManager;

            SoundEffect music = _gameManager.ServiceLocator.Get<IContentFactory>().MenuBackgroundMusic;
            soundEffectInstance = music.CreateInstance();
            soundEffectInstance.IsLooped = true;
            soundEffectInstance.Play();

            MenuEntry playGameMenuEntry = new MenuEntry( gameManager, Constants.Strings.PLAY_GAME );
            MenuEntry showNotificationMenuEntry = new MenuEntry(gameManager, "Show Notification Test");
            MenuEntry exitMenuEntry = new MenuEntry( gameManager, Constants.Strings.EXIT );
            

            playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
            showNotificationMenuEntry.Selected += OnShowNotification;
            exitMenuEntry.Selected += OnCancel;

            MenuEntries.Add( playGameMenuEntry );
            MenuEntries.Add(showNotificationMenuEntry);
            MenuEntries.Add( exitMenuEntry );
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if ( soundEffectInstance.State != SoundState.Playing )
            {
                soundEffectInstance.Resume();
            }
        }

        private void OnShowNotification(object sender, PlayerIndexEventArgs e)
        {
            ScreenManager.AddScreen(_gameManager, new NotificationScreen("Achievement unlocked!", 3), null);
        }

        void PlayGameMenuEntrySelected( object sender, PlayerIndexEventArgs e )
        {
            LoadingScreen.Load( _gameManager, true, e.PlayerIndex, new GameplayScreen() );
        }

        protected override void OnCancel( PlayerIndex playerIndex )
        {
            _gameManager.Game.Exit();
        }
    }
}
